package cn.b.sky.mes.em.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.em.dao.EquipmentDownTimeDao;
import cn.b.sky.mes.em.model.EquipmentDownTime;
import cn.b.sky.mes.em.model.condition.EquipmentDownTimeCondition;
import cn.b.sky.mes.em.service.EquipmentDownTimeService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("equipmentDownTimeService")
public class EquipmentDownTimeServiceImol implements EquipmentDownTimeService {

  private static final Logger LOGGER = LoggerFactory.getLogger(EquipmentDownTimeServiceImol.class);

  @Resource
  EquipmentDownTimeDao equipmentDownTimeDao;

  @Override
  public List<EquipmentDownTime> selectEquipmentDownTimeListByCondition(EquipmentDownTimeCondition condition) {
    if (condition == null) {
      condition = new EquipmentDownTimeCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return equipmentDownTimeDao.selectEquipmentDownTimeListByCondition(condition);
  }

  @Override
  public int selectEquipmentDownTimeCountByCondition(EquipmentDownTimeCondition condition) {
    if (condition == null) {
      condition = new EquipmentDownTimeCondition();
    }
    return equipmentDownTimeDao.selectEquipmentDownTimeCountByCondition(condition);
  }

  @Override
  public EquipmentDownTime getEquipmentDownTime(Long id) {
    if (id == null) {
      return null;
    }
    return equipmentDownTimeDao.getEquipmentDownTime(id);
  }

  @Override
  public boolean updateEquipmentDownTime(EquipmentDownTime time) {
    if (time.getId() == null) {
      return false;
    }
    equipmentDownTimeDao.updateEquipmentDownTime(time);
    return true;
  }

  @Override
  public boolean insertEquipmentDownTime(EquipmentDownTime time) {
    equipmentDownTimeDao.insertEquipmentDownTime(time);
    return true;
  }

  @Override
  public BatchJobReport deleteEquipmentDownTime(Long id) {
    if (id != null) {
      String message = "";
      String cause = "";
      boolean success = true;
      try {
        equipmentDownTimeDao.deleteEquipmentDownTime(id);
      } catch (Exception e) {
        LOGGER.error("{}", e);
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(e.getMessage())) {
          //外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
              SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                  SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        message = this.getEquipmentDownTime(id).getEquipmentCode();
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success);
    } else {
      return new BatchJobReport("", "equipmentCode is null", false);
    }
  }

}
